import Vue from "vue";
import Router from "vue-router";
import Home from "../views/Home.vue"
import GoodsDetail from "../views/GoodsDetail.vue"
import My from "../views/My.vue"
import Login from "../views/login.vue"
import AboutUs from "../views/AboutUs.vue"
import MyFavorites from "../views/MyFavorites.vue"
import ShoppingCar from "../views/ShoppingCar.vue"
import Category from "../views/Category.vue"
import brandGoods from "../views/brandGoods.vue"
import search from "../views/search.vue"
import Reg from "../views/Reg.vue"
import captcha from "../views/captcha.vue"
import pwd from "../views/pwd.vue"
import index from "../views/index.vue"
import MySet from "../views/MySet.vue"
import MyOrderView from "../views/MyOrderView.vue"
import MyAddressManage from "../views/MyAddressManage.vue"
import MyAddAddress from "../views/MyAddAddress.vue"
import NotFound from "../views/NotFound.vue"




// 把插件安装到Vue里
Vue.use(Router);

let router = new Router({
    routes: [
        {
            path: "/",
            component: Home
        },
        {
            path: "/Home",
            component: Home,
        },
        {
            path: "/Reg",
            component: Reg,
            children: [
                {
                    path: "/Reg/index",
                    component: index,
                },
                {
                    path: "captcha",
                    component: captcha,
                },
                {
                    path: "pwd",
                    component: pwd,
                }
            ]
        },
        {
            path: "/GoodsDetail/:goodsid",
            component: GoodsDetail
        },
        {
            path: "/My",
            component: My,
            // redirect:"/My"
        },
        {
            path: "/MyOrderView",
            component: MyOrderView,
            // redirect:"/My"
        },
        {
            path: "/MyAddressManage",
            component: MyAddressManage,
            // redirect:"/My"
        },
        {
            path: "/MyAddAddress",
            component: MyAddAddress,
            // redirect:"/My"
        },
        {
            path: "/ShoppingCar",
            component: ShoppingCar
        },
        {
            path: "/Category",
            component: Category
        },
        {
            path: "/Login",
            component: Login
        },
        {
            path: "/AboutUs",
            component: AboutUs
        },
        {
            path: "/MyFavorites",
            component: MyFavorites
        },
        {
            path: "/brandGoods",
            component: brandGoods
        },
        {
            path: "/search",
            component: search
        },
        {
            path: "/MySet",
            component: MySet
        },
        {
            path:"*",
            component:NotFound
        }
    ]
});


// 路由守卫
router.beforeEach((to, from, next) => {
    console.log("全局守卫的钩子函数");
    console.log("to",to);
    console.log("from",from);
    if(to.path === "/ShoppingCar"){
        if(!localStorage.getItem("username")){
            // next("/ShoppingCar")
            next("/Login?path="+to.path)
        }
    }
        // 该跳哪里跳哪里
        next()
    
})
export default  router;